<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Data extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->_data["menuHL"] = "Data";
        $this->addBreadCrumb("Data", site_url("data"));
    }

    public function index() {
        redirect("data/statistik");
    }

    public function spasial() {
        $this->_data["useMap"] = true;
        $this->addBreadCrumb("Spasial", site_url("data/spasial"));
        
        $this->load->model("Region_model");
        $this->_data["allReg"] = $this->Region_model->findAll();
        
        $this->_renderPage("data/spasial");
    }

    public function statistik() {
        $this->_data["pageTitle"] = "Statistik";
        $this->_data["kode"] = 1101;

        $this->load->database("data_ps");

        $allSource = Array();
        $sql = "SELECT source_id, source.name FROM data, source, form WHERE data.source_id = source.id AND data.form_id = form.id AND data.is_deleted = 0 AND form.is_deleted = 0 AND form.is_approved = 1 GROUP BY source_id";
        $query = $this->db->query($sql);
        $allSource = $query->result();
        $this->_data["allSource"] = $allSource;

        $allYear = Array();
        $sql = "SELECT year FROM data, form WHERE data.form_id = form.id AND data.is_deleted = 0 AND form.is_deleted = 0 AND form.is_approved = 1 GROUP BY year";
        $query = $this->db->query($sql);
        $allYear = $query->result();
        $this->_data["allYear"] = $allYear;

        $allIndicator = Array();
        $sql = "SELECT q1.*, indicator.name as parent_name FROM (SELECT indicator.id, indicator.name, parent_id, indicator.metric FROM data, indicator, form WHERE data.indicator_id = indicator.id AND data.form_id = form.id AND data.is_deleted = 0 AND form.is_deleted = 0 AND form.is_approved = 1 AND indicator.has_value = 1 GROUP BY indicator_id) q1, indicator WHERE q1.parent_id = indicator.id ORDER BY indicator.id ASC";
        $query = $this->db->query($sql);
        foreach ($query->result() as $row) {
            if (!array_key_exists($row->parent_id . ":" . $row->parent_name, $allIndicator)) {
                $allIndicator[$row->parent_id . ":" . $row->parent_name] = Array();
            }
            array_push($allIndicator[$row->parent_id . ":" . $row->parent_name], $row);
        }
        $this->_data["allIndicator"] = $allIndicator;

        $kabList = Array();
        $sql = "SELECT year FROM data, form WHERE data.form_id = form.id AND data.is_deleted = 0 AND form.is_deleted = 0 AND form.is_approved = 1 GROUP BY year";
        $query = $this->db->query($sql);
        $kabList = $query->result();


        $this->_renderPage("data/statistik");
    }

    public function banPS() {
        $this->_data["pageTitle"] = "Data Bantuan Stimulan Perdesaan Sehat";
        $this->addBreadCrumb("Bantuan Stimulan PS", site_url("data/banps"));
        $this->_data["useMap"] = true;

        // fetch data kabupaten
        $this->load->model("Kabupaten_model");
        $this->_data["kabList"] = $this->Kabupaten_model->readSummarized(Array());

        $this->_renderPage("data/banps");
    }

    public function masukBanPS() {
        $this->load->database("data_ps");
        $this->db->from("banps");
        $query = $this->db->get();
        $this->_data["banPS"] = $query->result();
        $this->_renderPage("data/masukbanps");
    }

    public function addBanPS() {
        $this->load->database("data_ps");
        $this->db->from("kabupaten_banps");
        $this->db->where(Array(
            "kabupaten_code" => $this->input->post("kab"),
            "banps_id" => $this->input->post("banps")
        ));
        $query = $this->db->get();
        if (count($query->result()) < 1) {
            $this->db->insert("kabupaten_banps", Array(
                "kabupaten_code" => $this->input->post("kab"),
                "banps_id" => $this->input->post("banps")
            ));
        }
    }

    public function delBanPS() {
        $this->load->database("data_ps");
        $this->db->delete("kabupaten_banps", Array(
            "kabupaten_code" => $this->input->post("kab"),
            "banps_id" => $this->input->post("banps")
        ));
    }

}
